const { ccclass, property } = cc._decorator;

import l from "./EventMgr"
import s from "./NativeEventType"
// import c from "./SdkHelper"
import u from "./EngineUtil"
import p from "./PageConfig"
import d from "./PageMgr"
import EventMgr from "./EventMgr";

export enum AnimType {
    NONE = 0,
    SCALE = 1,
    FADE = 2,
    LEFT_TO_RIGHT = 3,
    BOTTOM_TO_TOP = 4,
    BLACK = 5
}

@ccclass
export default class BasePageCtrl extends cc.Component {
    @property({
        tooltip: "是否加入队列",
        visible: true
    })
    _inQueue = false;

    @property({
        tooltip: "是否唯一",
        visible: true
    })
    _only = true;

    @property({
        tooltip: "是否复用",
        visible: true
    })
    _reuse = true;

    @property({
        type: cc.Enum(AnimType),
        visible: true,
        tooltip: "动画类型"
    })
    _animType = AnimType.SCALE;

    @property({
        visible: true,
        tooltip: "动画时间"
    })
    _animTime = 0.3;

    @property({
        visible: true,
        displayName: "蒙版配置"
    })
    _animControl = false;

    @property({
        tooltip: "蒙版初始透明度",
        visible: function () {
            return this._animControl;
        }
    })
    _black_start_opacity = 0;

    @property({
        tooltip: "蒙版结束透明度",
        visible: function () {
            return this._animControl;
        }
    })
    _black_end_opacity = 200;

    @property({
        tooltip: "蒙版动画时间",
        visible: function () {
            return this._animControl;
        }
    })
    _blackTime = 0.3;

    @property({
        visible: true,
        displayName: "页面遮挡控制"
    })
    _touchControl = false;

    @property({
        tooltip: "阻止页面穿透",
        visible: function () {
            return this._touchControl;
        }
    })
    _hasPeneLock = true;

    @property({
        tooltip: "黑色蒙版",
        visible: function () {
            return this._touchControl;
        }
    })
    _hasBlack = true;

    @property({
        tooltip: "阻止页面内点击",
        visible: function () {
            return this._touchControl;
        }
    })
    _hasTouchLock = true;

    @property({
        tooltip: "页面黑色遮罩Touch监听",
        visible: function () {
            return this._touchControl;
        }
    })
    _hasBlackTouch = true;

    @property({
        displayName: "显示内容尺寸是否设置为屏幕大小"
    })

    protected ui;
    _showWinSize = false;
    public _set_oldContent = new Set();
    public _peneLock = null;
    public _black = null;
    public _touchLock = null;
    public _content = null;
    public _highestIndex = 0;
    public _show_timestemp = 0;
    public _report_data = null;
    public onLoad() {
        this._saveContent();
        this._createPeneLock();
        this._createBlack();
        this._createContent();
        this._createTouchLock();
        l.listen(s.APP_PAUSE, this._onAppPause, this);
        l.listen(s.APP_RESTART, this._onAppRestart, this);
    };

    public onEnable() {
        this._reportEntry();
        this._show();
    };
    public onDisable() {
        this._reportExit();
        d.hidePage(this.node.name);
    };
    public _addReportData(e) {
        this._report_data || (this._report_data = {});
        Object.assign(this._report_data, e);
    };
    public _onAppPause() {
        this.node.active && this._reportExit();
    };
    public _onAppRestart() {
        this.node.active && this._reportEntry();
    };
    public _reportEntry() {
        this._show_timestemp = new Date().getTime();
        var e = {
            act_page: this.node.name
        };
        this._report_data && Object.assign(e, this._report_data);
        // c.reportData("b_entry_game_page", e);
    };
    public _reportExit() {
        var e = new Date().getTime();
        // c.reportData("b_leave_game_page", {
        //     act_page: this.node.name,
        //     duration: e - this._show_timestemp
        // });
        this._show_timestemp = 0;
    };
    public _init() { };
    public onUILoad() { };
    public _show() {
        var e = this, t = this._animType;
        this._lockTouch();
        var a = this._content;
        cc.Tween.stopAllByTarget(a);
        var o = this._animTime;
        switch (t) {
            case AnimType.NONE:
                this._onShow();
                break;

            case AnimType.SCALE:
                a.scale = 0;
                cc.tween(a).to(o, {
                    scale: 1
                }, {
                    easing: "backOut"
                }).call(function () {
                    e._onShow();
                }).start();
                break;

            case AnimType.FADE:
                a.opacity = 0;
                cc.tween(a).to(o, {
                    opacity: 255
                }).call(function () {
                    e._onShow();
                }).start();
                break;

            case AnimType.LEFT_TO_RIGHT:
                var i = a.position, r = cc.winSize;
                a.x -= .5 * r.width;
                cc.tween(a).to(o, {
                    x: i.x
                }, {
                    easing: "sineOut"
                }).call(function () {
                    e._onShow();
                }).start();
                break;

            case AnimType.BOTTOM_TO_TOP:
                var l = a.position, s = cc.winSize;
                a.y -= .5 * s.height;
                cc.tween(a).to(o, {
                    x: l.x,
                    y: l.y
                }, {
                    easing: "sineOut"
                }).call(function () {
                    e._onShow();
                }).start();
        }
        this._showBlack();
    };

    private hideTimeout;
    public hide() {
        if (this.hideTimeout) {
            return; // 如果已经设置了延迟调用，则直接返回
        }
        this.hideTimeout = setTimeout(() => {
            this.hideTimeout = undefined; // 清除延迟调用标识
        }, 500); // 指定的时间间隔，例如 500 毫秒
        var e = this, t = this._animType;
        this._lockTouch();
        var a = this._content;
        cc.Tween.stopAllByTarget(a);
        var o = this._animTime;
        switch (t) {
            case AnimType.NONE:
                this._onHide();
                break;

            case AnimType.SCALE:
                a.scale = 1;
                cc.tween(a).to(o, {
                    scale: 0
                }, {
                    easing: "backIn"
                }).call(function () {
                    e._onHide();
                }).start();
                break;

            case AnimType.FADE:
                a.opacity = 255;
                cc.tween(a).to(o, {
                    opacity: 0
                }).call(function () {
                    e._onHide();
                }).start();
                break;

            case AnimType.LEFT_TO_RIGHT:
                this._onHide();

            case AnimType.BOTTOM_TO_TOP:
                this._onHide();
        }
        this._hideBlack();
    };
    public _onShow() {
        this._unLockTouch();
        this.addBlackTouch();
        const pageList = [
            "MatchGameTargetPage_2",
            "MatchGameMoneyPage_3",
            "MatchGameSuccessJJPage_5",
            "MatchGameFailPage_6",
            "MatchGameAddPropPage_7",
            "MatchGameSetPage_8",
            "ClickEveryDayPage_9"
        ];
        for (const page of pageList) {
            if (page.toLowerCase().includes(this.node.name.toLowerCase())) {
                const pageNumber = parseInt(page.split("_")[1]);
                try{
                    if(this.node.active){
                        this.node.getChildByName('content').y = 200;
                        EventMgr.trigger("showAdImage",pageNumber);
                    }
                }catch(e){
                    //TODO handle the exception
                }
                
                break; // 如果找到匹配页面，可以选择中断循环
            }
        }
      
    };
    public _onHide() {
        this._unLockTouch();
        if (this._reuse) this.node.active = false; else {
            this.node.destroy();
            console.log("destroy");
        }
        EventMgr.trigger("hideAdImage");
    };
    public _showBlack() {
        var e = this._black;
        if (e) {
            e.opacity = this._black_start_opacity;
            cc.Tween.stopAllByTarget(e);
            cc.tween(e).to(this._blackTime, {
                opacity: this._black_end_opacity
            }).start();
        }
    };
    public _hideBlack() {
        var e = this._black;
        if (e) {
            e.opacity = this._black_end_opacity;
            cc.Tween.stopAllByTarget(e);
            cc.tween(e).to(this._blackTime, {
                opacity: this._black_start_opacity
            }).start();
        }
    };
    public _unLockTouch() {
        this._touchLock && (this._touchLock.active = false);
    };
    public _lockTouch() {
        this._touchLock && (this._touchLock.active = true);
    };
    public _saveContent() {
        var e = this;
        this.node.children.forEach(function (t) {
            return e._set_oldContent.add(t);
        });
    };
    public _setIndex(e) {
        if (e) {
            e.zIndex = this._highestIndex;
            this._highestIndex++;
        }
    };
    public _createContent() {
        var e = this, t = new cc.Node("content");
        this.node.addChild(t);
        this._set_oldContent.forEach(function (a) {
            a.parent = t;
            "top" == a.name && e._setTopNodes(a);
            "bottom" == a.name && e._setBottomNodes(a);
        });
        this._content = t;
        this._showWinSize && this._content.setContentSize(cc.winSize);
        this._setIndex(t);
    };
    public _setTopNodes(e) {
        var t = cc.winSize.height;
        e.y = u.isLargeScreen() ? t / 2 - p.DISTANCE_TOP : t / 2;
    };
    public _setBottomNodes(e) {
        var t = cc.winSize.height, a = u.isLargeScreen() ? t / 2 - p.DISTANCE_BOTTOM : t / 2;
        e.y = -a;
    };
    public _createTouchLock() {
        if (this._hasTouchLock) {
            var e = new cc.Node("closeTouch");
            e.addComponent(cc.BlockInputEvents);
            e.setContentSize(cc.winSize);
            this.node.addChild(e);
            this._touchLock = e;
            this._setIndex(e);
        }
    };
    public _createPeneLock() {
        if (this._hasPeneLock) {
            var e = new cc.Node("peneLock");
            e.addComponent(cc.BlockInputEvents);
            e.setContentSize(cc.winSize);
            this.node.addChild(e);
            this._peneLock = e;
            this._setIndex(e);
        }
    };
    public _createBlack() {
        if (this._hasBlack) {
            var e = new cc.Node("black"), t = e.addComponent(cc.Sprite);
            cc.resources.load("imgs/common/back", cc.SpriteFrame, function (a, o) {
                if (a) console.error("class:basePage", a); else {
                    t.spriteFrame = o;
                    e.setContentSize(cc.winSize);
                }
            });
            this.node.addChild(e);
            e.color = u.getColor("000000");
            this._black = e;
            this._setIndex(e);
        }
    };
    public addBlackTouch() {
        if (this._hasBlackTouch && this._black) {
            this._black.hasEventListener(cc.Node.EventType.TOUCH_END) && this.removeBlackTouch();
            this._black.on(cc.Node.EventType.TOUCH_END, this.onBlackTouch, this);
        }
    };
    public removeBlackTouch() {
        this._black && this._black.off(cc.Node.EventType.TOUCH_END, this.onBlackTouch, this);
    };
    public onBlackTouch() {
        this.removeBlackTouch();
        this.hide();
    };
}